<?php
include_once('../lib/config.php');

/* rename_character
 * Parameters: $char_id - the ID of the character you want to adress
 *             $name    - New name of character
 * Return value: true on success, false on database failure
 *
 * This changes one character's name in the system(tier) you are
 * currently active as administrator in.
 */
function rename_character($char_id, $new_name) {
    $system_id = @$system["system_id"];
    $sql = "update wm_characters
            set character_name = '$new_name'
            where character_system_id = $system_id and
                  character_id = $char_id";
    $result = $db->query($sql);
    if ($result == 1)
        return true;
    return false;
}

/* approve_character
 * Parameters: $char_id - the ID of the character you want to approve
 * Return value: true on success, false on database failure
 *
 * Sets a character as approved within the system you are active in
 * as administrator.
 */ 
function approve_character($char_id) {
    $system_id = @$system["system_id"];
    $sql = "update wm_characters
            set character_approved = 1
            where character_system_id = $system_id and
                  character_id = $char_id";
    $result = $db->query($sql);
    if ($result == 1)
        return true;
    return false;
}
    
/* search_characters
 * Paramters: $approved - toggle searching approved or non-approved
 *                        characters. Default approved = true
 * Return value: An iterator over returned database rows
 *
 * Search through characters within your active system.
 *
 * TODO: Expand the parameter list of the function to support more 
 *       filters.
 */
function search_characters($approved = true) {
    $system_id = @$system["system_id"];
    $where = "where character_system_id = $system_id";
    if ($approved) {
        $where .= " and character_approved = 1";
    } else {
        $where .= " and character_approved = 0";
    }
    $sql = "select character_id, character_name
            from wm_characters
            $where";
    $result = $db->query($sql);
    return $db->fetch_assoc($result);
}

/* character_exsists
 * Parameters: $char - a character's name
 * Return value: true if found, false if not
 *
 * Check for name within system.
 */
function character_exists($char) {
    $system_id = @$system["system_id"];
    $sql = "select character_id
            from wm_characters
            where character_system_id = $system_id and 
                  character_name like $char";
    $result = $db->query($sql);
    if($db->num_rows($result)>0)
        return true;
    return false;
}

?>